home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Programming / Programming Tools / Pascal Demos from Apple / qd samples / SAMP.TEXT < prev   
Encoding:
Text File  |  1985-03-27  |  9.6 KB  |  203 lines  |  [TEXT/ttxt]

  1. PROGRAM Sample;
  2.  
  3. { Sample -- A small sample application written by Macintosh User Education }
  4. { It displays a single, fixed-size window in which the user can enter and edit text. }
  5.  
  6. { The following two compiler commands are required for the Lisa Workshop. }
  7. {$X-}  {turn off automatic stack expansion}
  8. {$U-}  {turn off Lisa libraries}
  9.  
  10. { The USES clause brings in the units containing the Pascal interfaces. }
  11. { The $U expression tells the compiler what file to look in for the specified unit. }
  12. USES {$U Obj/MemTypes } MemTypes,   {basic Memory Manager data types}
  13.      {$U Obj/QuickDraw} QuickDraw,  {interface to QuickDraw}
  14.      {$U Obj/OSIntf   } OSIntf,     {interface to the Operating System}
  15.      {$U Obj/ToolIntf } ToolIntf;   {interface to the Toolbox}
  16.  
  17. CONST appleID = 128;     {resource IDs/menu IDs for Apple, File, and Edit menus}
  18.       fileID  = 129;
  19.       editID  = 130;
  20.  
  21.       appleM = 1;        {index for each menu in myMenus (array of menu handles)}
  22.       fileM  = 2;
  23.       editM  = 3;
  24.  
  25.       menuCount = 3;     {total number of menus}
  26.  
  27.       windowID = 128;    {resource ID for application's window}
  28.  
  29.       undoCommand  = 1;  {menu item numbers identifying commands in Edit menu}
  30.       cutCommand   = 3;
  31.       copyCommand  = 4;
  32.       pasteCommand = 5;
  33.       clearCommand = 6;
  34.  
  35. VAR myMenus: ARRAY[1..menuCount] OF MenuHandle; {array of handles to the menus}
  36.     dragRect: Rect;         {rectangle used to mark boundaries for dragging window}
  37.     txRect: Rect;           {rectangle for text in application window}
  38.     textH: TEHandle;        {handle to information about the text}
  39.     theChar: CHAR;          {character typed on the keyboard or keypad}
  40.     extended: BOOLEAN;      {TRUE if user is Shift-clicking}
  41.     doneFlag: BOOLEAN;      {TRUE if user has chosen Quit command}
  42.     myEvent: EventRecord;   {information about an event}
  43.     wRecord: WindowRecord;  {information about the application window}
  44.     myWindow: WindowPtr;    {pointer to wRecord}
  45.     whichWindow: WindowPtr; {pointer to window in which mouse button was pressed}
  46.  
  47.  
  48. PROCEDURE SetUpMenus;
  49. { Set up menus and menu bar }
  50.  
  51.   VAR i: INTEGER;
  52.  
  53.   BEGIN
  54.   { Read menu descriptions from resource file into memory and store handles }
  55.   { in myMenus array }
  56.   myMenus[appleM] := GetMenu(appleID); {read Apple menu from resource file}
  57.   AddResMenu(myMenus[appleM],'DRVR');  {add desk accessory names to Apple menu}
  58.   myMenus[fileM] := GetMenu(fileID);   {read File menu from resource file}
  59.   myMenus[editM] := GetMenu(editID);   {read Edit menu from resource file}
  60.  
  61.   FOR i:=1 TO menuCount DO InsertMenu(myMenus[i],0);  {install menus in menu bar }
  62.   DrawMenuBar;                                        { and draw menu bar}
  63.   END;    {of SetUpMenus}
  64.  
  65.  
  66. PROCEDURE DoCommand (mResult: LONGINT);
  67. { Execute command specified by mResult, the result of MenuSelect }
  68.  
  69.   VAR theItem: INTEGER;  {menu item number from mResult low-order word}
  70.       theMenu: INTEGER;  {menu number from mResult high-order word}
  71.       name: Str255;      {desk accessory name}
  72.       temp: INTEGER;
  73.  
  74.   BEGIN
  75.   theItem := LoWord(mResult);             {call Toolbox Utility routines to set }
  76.   theMenu := HiWord(mResult);             { menu item number and menu number}
  77.  
  78.   CASE theMenu OF                         {case on menu ID}
  79.  
  80.   appleID:
  81.     BEGIN                                  {call Menu Manager to get desk accessory }
  82.     GetItem(myMenus[appleM],theItem,name); { name, and call Desk Manager to open }
  83.     temp := OpenDeskAcc(name);             { accessory (OpenDeskAcc result not used)}
  84.     SetPort(myWindow);                     {call QuickDraw to restore application }
  85.     END;    {of appleID}                   { window as grafPort to draw in (may have }
  86.                                            { been changed during OpenDeskAcc)}
  87.   fileID:
  88.     doneFlag := TRUE;             {quit (main loop repeats until doneFlag is TRUE)}
  89.  
  90.   editID:
  91.     BEGIN                         {call Desk Manager to handle editing command if }
  92.     IF NOT SystemEdit(theItem-1)  { desk accessory window is the active window}
  93.       THEN                        {application window is the active window}
  94.         CASE theItem OF           {case on menu item (command) number}
  95.  
  96.         cutCommand:    TECut(textH);     {call TextEdit to handle command}
  97.         copyCommand:   TECopy(textH);
  98.         pasteCommand:  TEPaste(textH);
  99.         clearCommand:  TEDelete(textH);
  100.  
  101.         END;    {of item case}
  102.     END;    {of editID}
  103.  
  104.   END;    {of menu case}          {to indicate completion of command, call }
  105.   HiliteMenu(0);                  { Menu Manager to unhighlight menu title }
  106.                                   { (highlighted by MenuSelect)}
  107.   END;  {of DoCommand}
  108.  
  109.  
  110. BEGIN    {main program}
  111. { Initialization }
  112. InitGraf(@thePort);           {initialize QuickDraw}
  113. InitFonts;                    {initialize Font Manager}
  114. FlushEvents(everyEvent,0);    {call OS Event Manager to discard any previous events}
  115. InitWindows;                  {initialize Window Manager}
  116. InitMenus;                    {initialize Menu Manager}
  117. TEInit;                       {initialize TextEdit}
  118. InitDialogs(NIL);             {initialize Dialog Manager}
  119. InitCursor;                   {call QuickDraw to make cursor (pointer) an arrow}
  120.  
  121. SetUpMenus;                   {set up menus and menu bar}
  122. WITH screenBits.bounds DO     {call QuickDraw to set dragging boundaries; ensure at }
  123.   SetRect(dragRect,4,24,right-4,bottom-4); { least 4 by 4 pixels will remain visible}
  124. doneFlag := FALSE;            {flag to detect when Quit command is chosen}
  125.  
  126. myWindow := GetNewWindow(windowID,@wRecord,POINTER(-1)); {put up application window}
  127. SetPort(myWindow);            {call QuickDraw to set current grafPort to this window}
  128. txRect := thePort^.portRect;  {rectangle for text in window; call QuickDraw to bring }
  129. InsetRect(txRect,4,0);        { it in 4 pixels from left and right edges of window}
  130. textH := TENew(txRect,txRect);        {call TextEdit to prepare for receiving text}
  131.  
  132. { Main event loop }
  133. REPEAT                                {call Desk Manager to perform any periodic }
  134.   SystemTask;                         { actions defined for desk accessories}
  135.   TEIdle(textH);                      {call TextEdit to make vertical bar blink}
  136.  
  137.   IF GetNextEvent(everyEvent,myEvent) {call Toolbox Event Manager to get the next }
  138.     THEN                              { event that the application should handle}
  139.       CASE myEvent.what OF            {case on event type}
  140.  
  141.       mouseDown:            {mouse button down:  call Window Manager to learn where}
  142.         CASE FindWindow(myEvent.where,whichWindow) OF
  143.  
  144.         inSysWindow:        {desk accessory window:  call Desk Manager to handle it}
  145.           SystemClick(myEvent,whichWindow);
  146.  
  147.         inMenuBar:          {menu bar:  call Menu Manager to learn which command, }
  148.           DoCommand(MenuSelect(myEvent.where)); { then execute it}
  149.  
  150.         inDrag:             {title bar:  call Window Manager to drag}
  151.           DragWindow(whichWindow,myEvent.where,dragRect);
  152.  
  153.         inContent:                          {body of application window: }
  154.           BEGIN                             { call Window Manager to check whether }
  155.           IF whichWindow <> FrontWindow     { it's the active window and make it }
  156.             THEN SelectWindow(whichWindow)  { active if not}
  157.             ELSE
  158.               BEGIN                         {it's already active:  call QuickDraw to }
  159.               GlobalToLocal(myEvent.where); { convert to window coordinates for }
  160.                                             { TEClick, use Toolbox Utility BitAnd to }
  161.               extended := BitAnd(myEvent.modifiers,shiftKey) <> 0;  { test for Shift }
  162.               TEClick(myEvent.where,extended,textH);   { key down, and call TextEdit }
  163.               END;                                     { to process the event}
  164.           END;    {of inContent}
  165.  
  166.         END;    {of mouseDown}
  167.  
  168.       keyDown, autoKey:              {key pressed once or held down to repeat}
  169.         BEGIN
  170.         theChar := CHR(BitAnd(myEvent.message,charCodeMask));  {get the character}
  171.         IF BitAnd(myEvent.modifiers,cmdKey) <> 0  {if Command key down, call Menu }
  172.           THEN DoCommand(MenuKey(theChar))        { Manager to learn which command,}
  173.           ELSE TEKey(theChar,textH);              { then execute it; else pass }
  174.         END;                                      { character to TextEdit}
  175.  
  176.       activateEvt:
  177.         BEGIN
  178.         IF BitAnd(myEvent.modifiers,activeFlag) <> 0
  179.           THEN                     {application window is becoming active: }
  180.             BEGIN                  { call TextEdit to highlight selection }
  181.             TEActivate(textH);     { or display blinking vertical bar, and call }
  182.             DisableItem(myMenus[editM],undoCommand); { Menu Manager to disable }
  183.             END                    { Undo (since application doesn't support Undo)}
  184.           ELSE
  185.             BEGIN                  {application window is becoming inactive: }
  186.             TEDeactivate(textH);   { unhighlight selection or remove blinking }
  187.             EnableItem(myMenus[editM],undoCommand);  { vertical bar, and enable }
  188.             END;                   { Undo (since desk accessory may support it)}
  189.         END;   {of activateEvt}
  190.  
  191.       updateEvt:                                 {window appearance needs updating}
  192.         BEGIN
  193.         BeginUpdate(WindowPtr(myEvent.message)); {call Window Manager to begin update}
  194.         EraseRect(thePort^.portRect);            {call QuickDraw to erase text area}
  195.         TEUpdate(thePort^.portRect,textH);       {call TextEdit to update the text}
  196.         EndUpdate(WindowPtr(myEvent.message));   {call Window Manager to end update}
  197.         END;    {of updateEvt}
  198.  
  199.       END;    {of event case}
  200.  
  201. UNTIL doneFlag;
  202. END.
  203.